package com.concerneyes.mine.Task;

import android.content.Context;
import android.text.TextUtils;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.model.MyAdressModel;
import com.concerneyes.mine.model.MyInfoModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;
import util.ToastHelper;

/**
 * 产品相关网路请求
 */
public class MyInfoImpl {
    private Context context;
    private MyInfoListener listener;

    public MyInfoImpl(MyInfoListener listener) {
        this.listener = listener;
        this.context = CEApp.getInstance();
    }

    /**
     * 个人资料
     */
    public void myInfoImpl() {
        HashMap<String, String> params = new HashMap<>();
        params.put("id", PreferUtil.getUserId());
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                MyInfoModel model = null;
                if (data == null) {
                    ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    if (listener != null) {
                        listener.onMyInfoFail();
                    }
                    return;
                }

                if (data.getInt("code") == 200) {
                    try {
                        JSONObject object = new JSONObject(response);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, MyInfoModel.class);
                    } catch (Exception e) {
                    }
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onMyInfoFail();
                    }
                }
                if (model != null) {
                    if (listener != null) {
                        listener.onMyInfoSuccess(model);
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/user/profile", params, "POST");
    }

    /**
     * 个人资料更新
     */
    public void myInfoUpdateImpl(MyInfoModel model) {
        //TODO 接口返回500
        HashMap<String, String> params = new HashMap<>();
        params.put("id", PreferUtil.getUserId());
        if (!TextUtils.isEmpty(model.getNickname())) {
            params.put("nickname", model.getNickname());
        }
        if (!TextUtils.isEmpty(model.getRealName())) {
            params.put("realname", model.getRealName());
        }
        if (!String.valueOf(model.getAge()).equals("0")) {
            params.put("age", String.valueOf(model.getAge()));
        }
        if (!TextUtils.isEmpty(model.getGender())) {
            params.put("gender", model.getGender());
        }
        if (!TextUtils.isEmpty(model.getJob())) {
            params.put("job", model.getJob());
        }
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                MyInfoModel model = null;
                if (data == null) {
                    ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    if (listener != null) {
                        listener.onMyInfoUpdateFail();
                    }
                    return;
                }
                if (data.getInt("code") == 200) {
                    try {
                        JSONObject object = new JSONObject(response);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, MyInfoModel.class);
                    } catch (Exception e) {
                    }
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onMyInfoUpdateFail();
                    }
                }
                if (model != null) {
                    if (listener != null) {
                        listener.onMyInfoUpdateSuccess(model);
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/user/update", params, "POST");
    }


}
